<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        div {
            height: 100px;
            width: 100px;
            background-color: burlywood;
            position: absolute;
            top: 0px;
            left: 500px;
        }
        p {
            height: 500px;
            width: 10px;
            background-color: tomato;
            position: absolute;
            top: 0px;
            left: 0px;
        }
    </style>
</head>
<body>
    <div></div>
    <p></p>
    <script>
        const box = document.querySelector("div")
        const solid = document.querySelector("p")

        let clock;
        box.addEventListener("click", () => {
            
            clearInterval(clock)
            let end = solid.offsetWidth + solid.offsetLeft
            
            clock = setInterval(() => {
                let start = box.offsetLeft
                let speed = start >= 300 ? 10 : Math.ceil(start / 30)
                if (start > end) {
                    box.style.left = start - speed + "px"
                } else {
                    clearInterval(clock)
                    box.style.left = end + "px"
                }
                console.log(start)
            }, 30)
        })

    </script>
</body>
</html>